package com.example.configuration;

import com.example.oss.load.OssLoadConfiguration;
import com.example.oss.manager.OssExecuteManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2019/6/11
 */
@Configuration("configWebConfig")
public class WebConfiguration implements WebMvcConfigurer {


    /**
     * <h2>注册oss配置文件到spring中</h2>
     * @return
     */
    @Bean("OssLoadConfiguration")
    OssLoadConfiguration getOssLoadConfiguration(){
        return new OssLoadConfiguration();
    }

    /**
     * <h2>对象存储规则执行管理器</h2>
     * @return
     */
    @Bean("OssExecuteManager")
    OssExecuteManager getOssExecuteManager(){
        return new OssExecuteManager();
    }


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        //不显示为null的字段
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);

        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        //放到第一个
        converters.add(0, jackson2HttpMessageConverter);
    }
}